﻿Public Class inputPrompt
    Public Event okayClicked(ByVal input As String, ByVal flag As String)
    'Function Declarations

    'Private Function Delcarations

    'Developer Name: Jason Lyle
    'Module Name: Mouse Control
    'Task(s): N/A
    'Pre-Condition: The clickstream exists
    'Post-Condition: The clickstream has nIputs more inputs, represented by
    '		the array pInputs, where pInputs is of size cbSize
    'Functionality: The inputs stored in pInputs are placed into the clickstream
    Private Declare Function SendInput Lib "user32.dll" ( _
     ByVal nInputs As Integer, ByRef pInputs As INPUT_TYPE, _
     ByVal cbSize As Integer) As Integer

    'Structure Declarations

    'Private Structures

    'Developer Name: Jason Lyle
    'Module Name: Mouse control
    'Task(s):  N/A
    'Functionality: A structure used to represent system input
    Private Structure INPUT_TYPE
    		
    		'The type of input
        Public dwType As Integer
        
        'The information about the mouse input
        Public xi As MOUSEINPUT
    End Structure

    'Developer Name: Jason Lyle
    'Module Name: Mouse control
    'Task(s): N/A
    'Functionality: A structure used to represent mouse events
    Private Structure MOUSEINPUT
    		
    		'Change in the cursors x position
        Public dx As Integer
        
        'Change in the cursors y position
        Public dy As Integer
        
        'Not used, just set to 0
        Public mouseData As Integer
        
        'What kind of action is occurring
        Public dwFlags As Integer
        
        'Not used, just set to 0
        Public dwtime As Integer
        
        'Not used, just set to 0
        Public dwExtraInfo As Integer
    End Structure

    'Global Variables

    'Constant

    'Input types for the INPUT_TYPE structure
    'Module: Mouse control
    Private Const INPUT_MOUSE = 0
    Private Const INPUT_KEYBOARD = 1
    Private Const INPUT_HARDWARE = 2

    'Key sates for ProcessCmdKey and ProcessKeyPreview
    'Module: Mouse control
    Private Const WM_KEYDOWN As Integer = 256
    Private Const WM_SYSKEYDOWN As Integer = 260
    Private Const WM_KEYUP As Integer = 257
    Private Const WM_SYSKEYUP As Integer = 261

    'numerical representations of mouse events used by the INPUT_TYPE strructure
    'Module: Mouse Control
    Private Const MOUSE_MOVE = &H1
    Private Const MOUSE_LEFTDOWN = &H2
    Private Const MOUSE_LEFTUP = &H4

    'Non-constant
    Private callingFlag As String

    'Function Definitions
    'Constructors

    'Developer Name: Jason Lyle
    'Module Name: Input Box
    'Task(s): N/A
    'Pre-Condition: None
    'Post-Condition: A new inputPrompt is created and initialized
    'Functionality: Create a new inputPrompt
    Public Sub New()
    
    'A default contrstuctor for the class
        InitializeComponent()
        Me.Text = ""
        messageLbl.Text = ""
        callingFlag = ""
    End Sub

    'Developer Name: Jason Lyle
    'Module Name: Input Box
    'Task(s): N/A
    'Pre-Condition: None
    'Post-Condition: A new inputPrompt is created and initialized
    'Functionality: Create a new inputPrompt
    Public Sub New(ByVal msg As String, Optional ByVal title As String = "", _
    		Optional ByVal flag As String = "")
    
    'A Constructor that initializes the component based on the parameters
    '	from the user.
        InitializeComponent()
        Me.Text = title
        messageLbl.Text = msg
        callingFlag = flag
    End Sub
    
    'Event Handlers
    
    'Developer Name: Jason Lyle
    'Module Name: Input Box
    'Task(s): N/A
    'Pre-Condition: Input box has been loaded.
    'Post-Condition: The event signaling the okay button is called and then the
    '		form is closed.
    'Functionality: Raise the event that the okay button is clicked and close.
    Private Sub okBtn_Click(ByVal sender As System.Object, _
    		ByVal e As System.EventArgs) Handles okBtn.Click
        RaiseEvent okayClicked(inputText.displayText, callingFlag)
        Me.Close()
    End Sub

		'Developer Name: Jason Lyle
    'Module Name: Input Box
    'Task(s): N/A
    'Pre-Condition: Input box has been loaded.
    'Post-Condition: The form is closed.
    'Functionality: Close the form
    Private Sub cancelBtn_Click(ByVal sender As System.Object, _
    	ByVal e As System.EventArgs) Handles cancelBtn.Click
        Me.Close()
    End Sub

    'Overridden Functions

    'Developer Name: Jason Lyle
    'Module Name: Mouse control
    'Task(s): N/A
    'Pre-Condition: Some form of key down event has occured.
    'Post-Condition: The key down event has been trapped and handled
    '		appropriately.
    'Functionality: Trap keyboard keys and use the arrow keys and the enter
    '		key to simulate a mouse.
    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, _
    			ByVal keyData As Keys) As Boolean
				
				'If the function gets a keydown event...
        If (msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN) Then
						
						'If the key is the left arrow, move the mouse left
            If (keyData = Keys.Left) Or (keyData = Keys.NumPad4) Then
                keyMouseMove(-7, 0)
            
            'If the key is the right arrow, move the mouse right
            ElseIf (keyData = Keys.Right) Or (keyData = Keys.NumPad6) Then
                keyMouseMove(7, 0)
                
            'If the key is the up arrow, move the mouse up
            ElseIf (keyData = Keys.Up) Or (keyData = Keys.NumPad8) Then
                keyMouseMove(0, -7)
                
            'If the key is the down arrow, move the mouse down.
            ElseIf (keyData = Keys.Down) Or (keyData = Keys.NumPad2) Then
                keyMouseMove(0, 7)
            
            'If the key is the enter key, then click the mouse down
            ElseIf (keyData = Keys.Enter) Or (keyData = Keys.NumPad5) Then
                keyMouseDown(False)
            End If

        End If

				'Returning true keeps the event from bubbling up.
				'This means that the system is informed that the
				'event has been handled.
        Return True
    End Function

    'Developer Name: Jason Lyle
    'Module Name: Mouse control
    'Task(s): N/A
    'Pre-Condition: Some form of key up event has occured.
    'Post-Condition: The key up event has been trapped and handled 
    '		appropriately.
    'Functionality: Thrap keyboard keys up event to simulate a mouse.
    Protected Overrides Function ProcessKeyPreview( _
    		ByRef m As System.Windows.Forms.Message) As Boolean

				'If there is a key up event...
        If (m.Msg = WM_KEYUP) Or (m.Msg = WM_SYSKEYUP) Then

						'If that event is the enter key up, then insert a left mouse
						'		button up into the clickstream.
            If m.WParam = Keys.Enter Then
                keyMouseUp()
            End If

        End If

				'Returning true keeps the event from bubbling up.
				'This means that the system is informed that the
				'event has been handled.
        Return True
    End Function

    'Other Functions

    'Public Functions

    'Developer Name: Jason Lyle
    'Module Name: Mouse Control
    'Task(s): N/A
    'Pre-Condition: None
    'Post-Condition: If alsoUp is true, then a mouse left button down and mouse
    '		up are added into the clickstream.  If alsoUp is false, then just a
    '		mouse left button down is added into the clickstream.
    'Functionality: Simulate a mouse click.
    Public Sub keyMouseDown(ByVal alsoUp As Boolean)

				'Create an intput type object
        Dim inputEvents(0) As INPUT_TYPE

				'Set the members for a left mouse button down event.
        inputEvents(0).xi.dx = 0
        inputEvents(0).xi.dy = 0
        inputEvents(0).xi.mouseData = 0
        inputEvents(0).xi.dwFlags = MOUSE_LEFTDOWN
        inputEvents(0).xi.dwtime = 0
        inputEvents(0).xi.dwExtraInfo = 0
        inputEvents(0).dwType = INPUT_MOUSE

				'Insert the event into the clickstream
        SendInput(1, inputEvents(0), Len(inputEvents(0)))

				'If the flag alsoUp is true, also send a left mouse button up event.
        If alsoUp Then
            keyMouseUp()
        End If
    End Sub

    'Developer Name: Jason Lyle
    'Module Name: Mouse control
    'Task(s): N/A
    'Pre-Condition: None
    'Post-Condition: A mouse left button up is added to the clickstream.
    'Functionality: Simulate a mouse button coming up.
    Public Sub keyMouseUp()

				'Create an intput type object
        Dim inputEvents(0) As INPUT_TYPE

				'Set the members for a left mouse button up event.
        inputEvents(0).xi.dx = 0
        inputEvents(0).xi.dy = 0
        inputEvents(0).xi.mouseData = 0
        inputEvents(0).xi.dwFlags = MOUSE_LEFTUP
        inputEvents(0).xi.dwtime = 0
        inputEvents(0).xi.dwExtraInfo = 0
        inputEvents(0).dwType = INPUT_MOUSE

				'Insert the event into the clickstream
        SendInput(1, inputEvents(0), Len(inputEvents(0)))
    End Sub

    'Developer Name: Jason Lyle
    'Module Name: Mouse control
    'Task(s): N/A
    'Pre-Condition: The cursor exists on screen.
    'Post-Condition: The cursor moves from its starting location by x and
    '		y pixels.
    'Functionality: Simulate mouse movement.
    Public Sub keyMouseMove(ByVal x As Integer, ByVal y As Integer)

				'Create an intput type object
        Dim inputEvents(0) As INPUT_TYPE

				'Set the members for a left mouse movement event.
        inputEvents(0).xi.dx = x
        inputEvents(0).xi.dy = y
        inputEvents(0).xi.mouseData = 0
        inputEvents(0).xi.dwFlags = MOUSE_MOVE
        inputEvents(0).xi.dwtime = 0
        inputEvents(0).xi.dwExtraInfo = 0
        inputEvents(0).dwType = INPUT_MOUSE
        
				'Insert the event into the clickstream
        SendInput(1, inputEvents(0), Len(inputEvents(0)))
    End Sub
End Class